package com.vpa.graphics;

import javax.microedition.khronos.opengles.GL10;

import android.graphics.Paint;
import android.graphics.Rect;

class AnalogGauge extends Spot 
{
	public String[] ticks;
	private double value;
	private int min, max;
	public String units;
	public float degrees;
	Texture gauge, needle;
	private static int[][] ana_positions = new int[][]{{70, 88}, {46, 136}, {55, 193}, {84, 232}, {135, 257},
		{188, 247}, {229, 217}, {250, 165}, {250, 109}, {150, 70}};
	private static int[] rotters = new int[]{117, 90, 63, 30, 0, -30, -63, -90, -117};
	private Rect r = new Rect();
	private static final int MCHAR = 20;
	Paint tp;
    private boolean newt = true;
	
	public void setValue(double val)
	{
		value = val;
		recalcDegrees(value);
	}
	
	private void recalcDegrees(double v)
	{
		degrees = (float)-((v - min)/(double)(max - min))*238;
	}
	
	public AnalogGauge(Texture gauge_res, Texture needle_res, int min, int max, String title, String units)
	{
		type = 0;
		this.min = min;
		this.max = max;
		owidth = gauge_res.res.width;
		oheight = gauge_res.res.height;
		setDims(owidth, oheight, false);
		originalAspRat = owidth/(float)oheight;
		this.title = title;//GraphicsController.heiselify(title, 10);
		this.units = units;//GraphicsController.heiselify(units, 10);
		
		ticks = new String[9];
		for(int i = 0; i < 9; i++)
		{
			ticks[i] = String.format("%2f", i*(max-min)/(double)8 + min); 
		}
		
		gauge = gauge_res;
		needle = needle_res;
		drawables = new Texture[2];
		drawables[0] = gauge;
		drawables[1] = needle;
		
		readyToLoad = true;
	}
	
	protected Texture getGTexture()
	{
		return gauge;
	}
	
	protected Texture getNeedleTexture()
	{
		return needle;
	}
	
	public void draw(GL10 gl, GLView v)
	{
		if(newt || text == null || tp == null)
		{
			TPC ot;
			if((ot = GraphicsController.fontCache.get("Roboto-Regular.ttf" + text_size)) != null)
			{
				text = ot.text;
				tp = ot.p;
			}
			else
			{
				text = new GLText(gl, v.c.getAssets());
				tp = text.load("Roboto-Regular.ttf", text_size, 4, 4).p;
				GraphicsController.fontCache.put("Roboto-Regular.ttf" + text_size, new TPC(text, tp));
			}
		    newt = false;
		}
		if(overTrash)
			gl.glColor4f(1f, 0, 0, 1f);
		synchronized(this)
		{
			if(moving)
			{
				gl.glColor4f(1f, .45f, .45f, .8f);
			}
			gauge.draw(gl);
			if(!moving)
			{
				gl.glPushMatrix();
				gl.glTranslatef(needle.vertices[6] - Math.abs(needle.vertices[0] - needle.vertices[6])/2,
		    			needle.vertices[4] - Math.abs(needle.vertices[4] - needle.vertices[1])/2, 0);
		    	gl.glRotatef(degrees, 0, 0, 1);
		    	gl.glTranslatef(-needle.vertices[6] + Math.abs(needle.vertices[0] - needle.vertices[6])/2,
		    			-needle.vertices[4] + Math.abs(needle.vertices[4] - needle.vertices[1])/2, 0);
				needle.draw(gl);
				gl.glPopMatrix();
			}
			gl.glColor4f(1f, 1f, 1f, 1f);
		}
		gl.glColor4f(1f, 1f, 1f, 1f);
		gl.glBlendFunc( GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA );
		
		//GLView.glText.begin( 1f, 0f, 0f, 1f );
		for(int i = 0; i < ticks.length; i++)
		{
			gl.glPushMatrix();
			gl.glTranslatef(gauge.vertices[0] + ((float)getWidth())/owidth*ana_positions[i][0],
					gauge.vertices[1] + ((float)getHeight())/oheight*ana_positions[i][1], 0);
			gl.glRotatef(rotters[i], 0, 0, 1);
			gl.glTranslatef(-gauge.vertices[0] - ((float)getWidth())/owidth*ana_positions[i][0],
					-gauge.vertices[1] - ((float)getHeight())/oheight*ana_positions[i][1], 0);
            drawText(1f, 0f, 0f, 1f, ticks[i].substring(0, (ticks.length > 4) ? (4):(ticks.length)),
            		gauge.vertices[0] + ((float)getWidth())/owidth*ana_positions[i][0], 
            		gauge.vertices[1] + ((float)getHeight())/oheight*ana_positions[i][1], text);
			gl.glPopMatrix();
		}
		tp.getTextBounds(title, 0, (title.length() >= MCHAR) ? (MCHAR-1):(title.length()), r);
		int k = r.height();
		drawText(0f, 1f, 0f, 1f, title.substring(0, (title.length() >= MCHAR) ? (MCHAR-1):(title.length())),
        		gauge.vertices[0] + ((float)getWidth())/owidth*ana_positions[ana_positions.length - 1][0] - r.width()/2, 
        		gauge.vertices[1] + ((float)getHeight())/oheight*ana_positions[ana_positions.length - 1][1], text);
		tp.getTextBounds(units, 0, (units.length() >= 11) ? (10):(units.length()), r);
		drawText(0f, 1f, 0f, 1f, units.substring(0, (units.length() >= MCHAR) ? (MCHAR-1):(units.length())),
        		gauge.vertices[0] + ((float)getWidth())/owidth*ana_positions[ana_positions.length - 1][0] - r.width()/2,
        		gauge.vertices[1] + ((float)getHeight())/oheight*ana_positions[ana_positions.length - 1][1] - k - 2, text);
		//GLView.glText.end();
    	gl.glBlendFunc(GL10.GL_ONE, GL10.GL_ONE_MINUS_SRC_ALPHA);
	}
	
	public void move(int x, int y)
	{
		synchronized(this)
		{
			super.move(x, y);
		}
	}
	
	public void changeTextSize(int s)
	{
		if(text_size != s)
		{
			text_size = s;
			newt = true;
		}
	}
	
	protected void setDims(int w, int h, boolean b)
	{
		super.setDims(w, h, b);
		changeTextSize((int)(14/(float)300*h));
	}
}
